package com.coderman.daoclient.reflect;

import com.coderman.daoclient.utils.ClassUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${fanchunshuai} on 2017-7-28.
 *
 * @author fanchunshuai
 * @version: V1.0
 * @Desc:
 */
public class BeanMapInfo {
    private static volatile boolean init = false;
    /**
     * 存储所有字段
     * key:beanClass
     * value: List<bean.field>
     */
    private static Map<Class<?>,List<FieldInfo>> fieldMap = new HashMap<>(16);

    /**
     * 存储所有table名称
     */
    private static Map<Class<?>,String> tableMap = new HashMap<>(16);

    public static Map<Class<?>, List<FieldInfo>> getFieldMap() throws Exception {
        //防止java.util.ConcurrentModificationException
        if(fieldMap.isEmpty()){
            synchronized (fieldMap){
                ClassUtils.getClassFieldMap(fieldMap,tableMap);

            }
        }
        return fieldMap;
    }


    public static Map<Class<?>, String> getTableMap() throws Exception {
        if(tableMap.isEmpty()){
            synchronized (tableMap){
                ClassUtils.getClassFieldMap(fieldMap,tableMap);
            }
        }

        return tableMap;
    }

    /**
     * 初始化bean入口
     */
    public static void initBean() throws Exception {
        if(!init){
            System.out.println("init bean start ");
            init = true;
            getTableMap();
            getFieldMap();
            System.out.println("init bean end ");
        }
    }

}
